﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections;

namespace TestSuite
{


    /// <summary>
    ///This is a test class for ObservableDictionaryTest and is intended
    ///to contain all ObservableDictionaryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ObservableDictionaryTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ObservableDictionary`2 Constructor
        ///</summary>
        public void ObservableDictionaryConstructorTestHelper<TKey, TValue>()
        {
            ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>();
            Assert.IsNotNull(target.ValueObservableCollection);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
        }

        [TestMethod()]
        public void ObservableDictionaryConstructorTest()
        {
            ObservableDictionaryConstructorTestHelper<GenericParameterHelper, GenericParameterHelper>();
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        public void AddTestHelper<TKey, TValue>()
        {
            ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>();
            TKey key = default(TKey);
            TValue value = default(TValue);
            target.Add(key, value);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(1, target.ValueObservableCollection.Count);
            Assert.AreEqual<TValue>(target[key], target.ValueObservableCollection[0]);
        }

        [TestMethod()]
        public void AddTest()
        {
            AddTestHelper<int, int>();
        }

        /// <summary>
        ///A test for Clear
        ///</summary>
        public void ClearTestHelper<TKey, TValue>()
        {
            ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>();
            TKey key = default(TKey);
            TValue value = default(TValue);
            target.Add(key, value);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(1, target.ValueObservableCollection.Count);
            Assert.AreEqual<TValue>(target[key], target.ValueObservableCollection[0]);
            target.Clear();
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(0, target.ValueObservableCollection.Count);
        }

        [TestMethod()]
        public void ClearTest()
        {
            ClearTestHelper<int, int>();
        }

        /// <summary>
        ///A test for Remove
        ///</summary>
        public void RemoveTestHelper<TKey, TValue>()
        {
            ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>();
            TKey key = default(TKey);
            TValue value = default(TValue);
            target.Add(key, value);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(1, target.ValueObservableCollection.Count);
            Assert.AreEqual<TValue>(target[key], target.ValueObservableCollection[0]);

            bool expected = true;
            bool actual;
            actual = target.Remove(key);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(0, target.ValueObservableCollection.Count);
        }

        [TestMethod()]
        public void RemoveTest()
        {
            RemoveTestHelper<int, int>();
        }

        /// <summary>
        ///A test for ValueObservableCollection
        ///</summary>
        public void ValueObservableCollectionTestHelper<TKey, TValue>()
        {
            ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>();
            TKey key = default(TKey);
            TValue value = default(TValue);
            target.Add(key, value);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(1, target.ValueObservableCollection.Count);

            ObservableCollection<TValue> actual;
            actual = target.ValueObservableCollection;
            Assert.IsNotNull(actual);
            Assert.AreEqual<TValue>(target[key], target.ValueObservableCollection[0]);
        }

        [TestMethod()]
        public void ValueObservableCollectionTest()
        {
            ValueObservableCollectionTestHelper<int, int>();
        }

        /// <summary>
        ///A test for ValueObservableCollection
        ///</summary>
        [TestMethod()]
        public void SortedCollectionTest()
        {
            ObservableDictionary<int, String> target = new ObservableDictionary<int, String>();

            int key = 3;
            String value = "30";
            target.Add(key, value);
            Assert.AreEqual<int>(target.Count, target.ValueObservableCollection.Count);
            Assert.AreEqual<int>(1, target.ValueObservableCollection.Count);
            Assert.AreEqual<String>(target[key], target.ValueObservableCollection[0]);

            key = 2;
            value = "20";
            target.Add(key, value);
            key = 5;
            value = "50";
            target.Add(key, value);
            key = 1;
            value = "10";
            target.Add(key, value);


            ICollectionView listView = CollectionViewSource.GetDefaultView(target.ValueObservableCollection);
            ListCollectionView listCollectionView = (ListCollectionView)listView;
            listCollectionView.CustomSort = new ValueSorter();

            // These lines prove that the view is functioning correctly
            listCollectionView.MoveCurrentToFirst();
            Assert.AreEqual<String>("10", listCollectionView.CurrentItem as String);
            listCollectionView.MoveCurrentToNext();
            Assert.AreEqual<String>("20", listCollectionView.CurrentItem as String);
            listCollectionView.MoveCurrentToNext();
            Assert.AreEqual<String>("30", listCollectionView.CurrentItem as String);
            listCollectionView.MoveCurrentToNext();
            Assert.AreEqual<String>("50", listCollectionView.CurrentItem as String);
            
        }
        public class ValueSorter : IComparer
        {
            public int Compare(object first, object second)
            {
                return ((String)first).CompareTo(second);
            }
        }
    }
}
